//
//  FQDQIconEmptyView.m
//  FakeQDaily
//
//  Created by mewxiv on 10/6/16.
//  Copyright © 2016 Xensius. All rights reserved.
//

#import "FQDQIconEmptyView.h"

@interface FQDQIconEmptyView ()

@property (nonatomic, strong) UIImageView *iconImageView;
@property (nonatomic, strong) UILabel *titleLabel;

@end

@implementation FQDQIconEmptyView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self prepareEmptyView];
    }
    return self;
}

- (void)layoutSubviews
{
    [self updateEmptyView];
}

- (void)prepareEmptyView
{
    UIImage *qIconImage = [UIImage imageNamed:@"favorite_default_empty"];
    CGRect iconFrame = (CGRect){CGPointZero, qIconImage.size};
    _iconImageView = [[UIImageView alloc] initWithFrame:iconFrame];
    _iconImageView.image = qIconImage;
    [self addSubview:_iconImageView];
    
    _titleLabel = [[UILabel alloc] init];
    _titleLabel.font = FQDRegularFontWithSize(17.0f);
    _titleLabel.textAlignment = NSTextAlignmentCenter;
    _titleLabel.textColor = RGB(134, 134, 134);
    [self addSubview:_titleLabel];
}

- (void)updateEmptyView
{
    CGSize viewSize = self.bounds.size;
    CGFloat aspectRatio = viewSize.width / viewSize.height;
    if (aspectRatio > 1) {
        CGFloat padding = 10;
        CGFloat iconCenterX = viewSize.width /  2;
        CGFloat iconCenterY = (viewSize.height - 2 * padding) / 2;
        self.iconImageView.center = CGPointMake(iconCenterX, iconCenterY);
    } else {
        CGFloat iconCenterX = viewSize.width / 2;
        CGFloat iconCenterY = viewSize.height / 2 - 10.0f;
        self.iconImageView.center = CGPointMake(iconCenterX, iconCenterY);
    }
    
    CGSize titleSize = self.titleLabel.frame.size;
    CGFloat titleX = CGRectGetMidX(self.iconImageView.frame) - titleSize.width / 2;
    CGFloat titleY = CGRectGetMaxY(self.iconImageView.frame) + 15;
    self.titleLabel.frame = CGRectMake(titleX, titleY, titleSize.width, titleSize.height);
}

- (void)setTitleText:(NSString *)titleText
{
    self.titleLabel.text = titleText;
    [self.titleLabel sizeToFit];
}

- (NSString *)titleText
{
    return self.titleLabel.text;
}

@end
